package com.junjie.food.orderServiceManager.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync // 允许使用异步
// 实现这个接口，重写方法，把线程池注入到springBoot
public class AsyncTaskConfig implements AsyncConfigurer {

    // 返回一个线程池
    @Bean
    @Override
    public Executor getAsyncExecutor(){
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(10); // 核心线程数
        threadPoolTaskExecutor.setMaxPoolSize(100);
        threadPoolTaskExecutor.setQueueCapacity(10); // 缓冲队列长度
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true); // 线程池关闭的时候需不要等所有线程结束
        threadPoolTaskExecutor.setAwaitTerminationSeconds(60); // 线程池等待关闭的时候，等待多久等线程结束
        threadPoolTaskExecutor.setThreadNamePrefix("Rabbit-Async-"); // 设置所有线程的名称前缀
        threadPoolTaskExecutor.initialize(); // 初始化
        return threadPoolTaskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}
